package wcc.engineeringAdmission.minified;
import java.util.*;
public class UserMainCode {
	public static String[] output1;
	private static class S {
		int r, m, i = 0, c;
		int[] p;
		public S(int r, int m, int[] p) {
			this.r = r;
			this.m = m;
			this.p = p;
		}
	}
	private static class C implements Comparator<S> {
		int r;
		public C(int r) {
			this.r = r;
		}
		public int compare(S s, S t) {
			if(s.r == r) {
				if(t.r == r)
					return s.m >= t.m ? -1 : 1;
				else
					return s.m > t.m * 0.7 ? -1 : 1;
			} else if(t.r == r)
				return t.m > s.m * 0.7 ? 1 : -1;
			else
				return s.m >= t.m ? -1 : 1;
		}
	}
	private static class I {
		int n;
		List<S> s = new ArrayList<S>(), p;
		C c;
		public I(int r, int n, List<S> p) {
			this.n = n;
			this.p = p;
			this.c = new C(r);
		}
		public boolean f() {
			return s.size() >= n;
		}
		public S w() {
			int i = -1;
			S w = null;
			for(S t : s) {
				int j = p.indexOf(t);
				if(j > i) {
					i = j;
					w = t;
				}
			}
			return w;
		}
		public boolean p(S s, S t) {
			return c.compare(s, t) < 0;
		}
	}
	private static String[] s(String t, char c) {
		int s = 0;
		Stack<Character> k = new Stack<Character>();
		List<String> r = new ArrayList<String>();
		for(int i = 0; i < t.length(); i++) {
			if(t.charAt(i) == c && k.isEmpty()) {
				r.add(t.substring(s, i));
				s = i + 1;
			}
			else if(t.charAt(i) == '(')
				k.push(t.charAt(i));
			else if(t.charAt(i) == ')')
				k.pop();
		}
		r.add(t.substring(s, t.length()));
		String[] e = new String[r.size()];
		return r.toArray(e);
	}
	private static HashMap<Integer, List<S>> g(S[] s) {
		HashMap<Integer, List<S>> g = new HashMap<Integer, List<S>>();
		for(int i = 0; i < s.length; i++) {
			for(int j = 0; j < s[i].p.length; j++) {
				List<S> l = g.get(s[i].p[j]);
				if(l == null) {
					l = new ArrayList<S>();
					g.put((Integer)s[i].p[j], l);
				}
				if(!l.contains(s[i]))
					l.add(s[i]);
			}
		}
		for(Integer k : g.keySet()) {
			List<S> d = g.get(k);
			C c = new C(k);
			Collections.sort(d, c);
		}
		return g;
	}
	private static void a(List<S> s, HashMap<Integer, I> i) {
		for(int j = 0; j < s.size();) {
			S t = s.get(j);
			if(t.i >= t.p.length) {
				j++;
				continue;
			}
			Integer r = (Integer)t.p[t.i++];
			I c = i.get(r);
			if(!c.f()) {
				t.c = r;
				c.s.add(t);
				s.remove(j);
			} else {
				S w = c.w();
				if(w != null && c.p(t, w)) {
					s.add(w);
					w.c = 0;
					c.s.remove(w);
					c.s.add(t);
					t.c = r;
					s.remove(j);
				}
			}
		}
	}
	public static void GetStudentCount(int a, String b) {
		String[] u = s(b, ',');
		output1 = new String[a];
		for(int t = 0; t < u.length; t++) {
			String[] k = s(u[t].substring(1, u[t].length() - 1), ',');
			int N = Integer.parseInt(k[0]);
			int M = Integer.parseInt(k[1]);
			String[] o = s(k[2].substring(1, k[2].length() - 1), ',');
			S[] s = new S[N];
			for(int i = 0; i < N; i++) {
				String[] v = s(o[i].substring(1, o[i].length() - 1), ',');
				int r = Integer.parseInt(v[0]);
				int m = Integer.parseInt(v[1]);
				int n = Integer.parseInt(v[2]);
				String[] f = v[3].substring(1, v[3].length() - 1).split(",");
				int[] h = new int[n];
				for(int j = 0; j < n; j++)
					h[j] = Integer.parseInt(f[j]);
				s[i] = new S(r, m, h);
			}
			HashMap<Integer, List<S>> g = g(s);
			String[] x = s(k[3].substring(1, k[3].length() - 1), ',');
			HashMap<Integer, I> z = new HashMap<Integer, I>();
			for(int i = 0; i < M; i++) {
				String[] y = x[i].substring(1, x[i].length() - 1).split(",");
				int r = Integer.parseInt(y[0]);
				z.put(r, new I(r, Integer.parseInt(y[1]), g.get(r)));
			}
			List<S> d = new ArrayList<S>(Arrays.asList(s));
			a(d, z);
			output1[t] = "";
			for(S h : s)
				output1[t] += h.c;
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String input = "7:(61,13,((2,97,9,(1,2,8,8,2,4,8,6,4)),(9,55,4,(6,2,2,8)),(3,41,6,(3,3,2,8,3,3)),(9,49,1,(8)),(2,86,9,(7,9,6,1,8,7,2,2,6)),(6,68,7,(6,2,9,4,6,2,1)),(9,95,3,(1,9,1)),(7,70,4,(3,4,5,2)),(9,64,1,(4)),(2,93,2,(7,2)),(7,59,3,(6,2,9)),(8,96,7,(6,8,7,5,3,7,6)),(5,29,7,(8,4,9,3,1,8,7)),(5,44,7,(8,9,9,4,9,1,6)),(6,34,1,(7)),(1,57,9,(6,7,2,7,5,5,2,8,4)),(3,22,6,(7,5,2,7,1,2)),(2,36,6,(7,1,9,7,9,8)),(9,83,8,(9,4,8,4,1,5,8,2)),(2,47,1,(8)),(4,85,4,(4,6,7,1)),(6,35,8,(3,9,9,9,8,4,4,8)),(4,37,2,(8,4)),(1,77,6,(9,3,8,9,4,1)),(6,84,5,(1,2,9,6,2)),(7,27,8,(3,2,2,8,5,9,5,6)),(2,45,4,(7,2,2,7)),(1,58,9,(8,7,6,3,6,3,4,1,6)),(1,65,6,(6,8,5,5,1,8)),(2,92,9,(4,8,8,6,2,5,7,9,7)),(2,38,5,(5,8,5,3,1)),(2,91,5,(8,2,4,2,3)),(9,90,5,(9,1,6,8,4)),(4,89,6,(9,1,2,9,7,5)),(3,48,4,(9,7,1,2)),(4,52,9,(6,5,2,7,8,9,3,5,1)),(9,53,6,(4,7,9,8,6,3)),(8,31,3,(8,4,8)),(6,43,6,(1,2,9,2,9,4)),(9,82,3,(2,4,2)),(2,99,8,(8,7,3,2,1,1,9,7)),(2,25,1,(3)),(9,33,7,(3,3,4,3,9,8,7)),(7,79,4,(7,5,5,2)),(8,63,8,(3,6,8,3,5,5,1,6)),(9,74,2,(5,7)),(1,88,5,(4,8,4,5,3)),(2,61,6,(1,3,2,2,2,9)),(7,81,7,(5,3,5,6,7,5,3)),(9,56,8,(8,4,1,2,4,2,9,2)),(6,24,6,(2,1,7,6,1,3)),(8,50,9,(2,5,5,7,3,8,9,7,4)),(5,26,3,(4,9,5)),(9,60,2,(4,3)),(9,54,4,(4,4,4,8)),(7,66,2,(3,8)),(7,69,9,(9,3,3,8,7,1,6,2,8)),(3,76,9,(2,7,8,1,3,1,9,7,5)),(8,21,7,(4,7,4,4,2,7,9)),(7,32,3,(3,5,6)),(9,72,4,(8,5,9,8))),((1,7),(2,1),(3,1),(4,5),(5,8),(6,2),(7,5),(8,3),(9,1),(10,8),(11,9),(12,3),(13,9)))" +
				",(30,18,((1,81,8,(6,3,6,5,9,4,2,6)),(1,75,7,(9,3,7,4,5,1,7)),(1,63,1,(3)),(9,45,1,(7)),(1,57,5,(5,1,9,3,6)),(2,92,4,(2,5,6,9)),(9,33,3,(5,8,5)),(6,61,4,(8,9,1,3)),(9,97,4,(8,6,9,2)),(6,93,9,(2,3,5,6,1,5,8,4,2)),(9,28,5,(5,9,2,8,9)),(5,69,9,(1,3,9,3,8,1,4,3,1)),(7,96,7,(9,2,1,9,5,1,4)),(7,68,9,(8,6,1,2,1,9,6,4,1)),(1,85,3,(9,9,1)),(5,83,5,(8,8,5,7,7)),(7,74,5,(3,6,3,1,8)),(7,55,4,(5,4,6,5)),(9,36,1,(4)),(9,26,2,(1,1)),(3,50,8,(8,8,1,6,6,3,3,2)),(7,94,2,(3,6)),(8,39,9,(1,3,4,9,5,5,9,6,6)),(2,22,7,(5,4,7,2,7,8,1)),(4,88,4,(9,6,3,4)),(5,46,8,(5,2,7,8,9,9,3,4)),(3,23,6,(6,8,2,8,4,7)),(7,76,6,(6,7,4,7,4,5)),(5,77,6,(6,8,4,3,7,5)),(7,34,3,(2,2,9))),((1,3),(2,4),(3,5),(4,3),(5,2),(6,3),(7,6),(8,4),(9,3),(10,2),(11,4),(12,5),(13,3),(14,6),(15,6),(16,9),(17,6),(18,8)))" +
				",(52,2,((2,50,1,(1)),(1,81,2,(1,1)),(1,95,2,(1,1)),(1,39,2,(2,2)),(1,66,2,(2,2)),(2,64,1,(2)),(2,49,2,(1,2)),(2,29,2,(1,1)),(2,91,2,(2,2)),(1,47,2,(1,1)),(2,87,2,(2,1)),(2,37,2,(2,1)),(1,35,2,(1,2)),(1,60,2,(1,2)),(2,74,1,(1)),(1,69,2,(1,1)),(1,32,2,(1,1)),(2,48,1,(1)),(1,52,1,(1)),(1,76,2,(1,1)),(1,78,2,(2,1)),(1,31,1,(1)),(2,55,1,(1)),(1,61,1,(2)),(2,67,1,(2)),(2,51,1,(1)),(2,82,2,(2,1)),(2,53,2,(2,2)),(2,75,2,(2,1)),(1,24,2,(1,2)),(1,28,1,(1)),(1,38,1,(2)),(1,57,2,(1,2)),(2,80,1,(1)),(2,65,1,(2)),(2,30,1,(2)),(2,25,1,(1)),(1,56,2,(2,2)),(1,79,2,(1,1)),(1,73,1,(2)),(2,59,2,(1,1)),(1,22,2,(2,2)),(1,85,1,(2)),(2,34,2,(2,1)),(2,21,2,(1,1)),(2,23,2,(2,2)),(2,41,2,(2,1)),(1,45,1,(2)),(1,89,1,(1)),(2,43,2,(2,2)),(1,27,2,(2,2)),(1,99,2,(2,2))),((1,4),(2,7)))" +
				",(50,9,((4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9)),(4,83,9,(5,3,4,6,8,7,2,1,9))),((1,6),(2,6),(3,6),(4,6),(5,6),(6,6),(7,6),(8,6),(9,6)))" +
				",(4,2,((1,100,2,(2,1)),(2,50,2,(1,2)),(2,60,1,(1)),(1,70,1,(2))),((1,1),(2,2)))" +
				",(2,2,((1,100,2,(2,1)),(2,90,2,(1,2))),((1,1),(2,1)))" +
				",(9,2,((1,100,2,(1,2)),(2,80,2,(2,1)),(1,90,1,(1)),(2,40,1,(2)),(2,50,1,(1)),(1,60,1,(2)),(2,75,1,(1)),(1,95,1,(1)),(2,30,1,(2))),((1,3),(2,4)))";
		String inputs[] = input.split(":");
		GetStudentCount(Integer.parseInt(inputs[0]), inputs[1]);
		System.out.println(Arrays.toString(output1));
	}
}
